package com.dk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dk.model.CraftRoute;
import com.dk.mapper.CraftRouteMapper;
import com.dk.service.CraftConstituteService;
import com.dk.service.CraftRelatedProductService;
import com.dk.service.CraftRouteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dk.vo.CraftConstituteVO;
import com.dk.vo.CraftRouteVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wbh
 * @since 2023-03-20
 */
@Service
public class CraftRouteServiceImpl extends ServiceImpl<CraftRouteMapper, CraftRoute> implements CraftRouteService {
    @Resource
    private CraftRouteMapper craftRouteMapper;
    @Autowired
    private CraftConstituteService craftConstituteService;

    @Autowired
    private CraftRelatedProductService craftRelatedProductService;

    @Override
    public List<CraftRouteVO> getInfoList(CraftRouteVO craftRouteVO) {
         /*工艺路线编号,艺路线名称,是否启用 模糊查询*/
        LambdaQueryWrapper<CraftRoute> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(craftRouteVO.getCraftRouteId()),CraftRoute::getCraftRouteId,craftRouteVO.getCraftRouteId());
        wrapper.like(StringUtils.isNotBlank(craftRouteVO.getCraftRouteName()),CraftRoute::getCraftRouteName,craftRouteVO.getCraftRouteName());
        wrapper.eq(StringUtils.isNotBlank(craftRouteVO.getCraftRouteFlag()),CraftRoute::getCraftRouteFlag,craftRouteVO.getCraftRouteFlag());
        List<CraftRouteVO> collect = craftRouteMapper.selectList(wrapper).stream().map(craftRoute -> {
            CraftRouteVO vo = new CraftRouteVO();
            BeanUtils.copyProperties(craftRoute, vo);
            return vo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public void insertInfoCraftRoute(CraftRouteVO craftRouteVO) {
        CraftRoute craftRoute = new CraftRoute();
        BeanUtils.copyProperties(craftRouteVO,craftRoute);
        craftRouteMapper.insert(craftRoute);
    }

    @Override
    public CraftRouteVO getInfoCraftRouteById(String craftRouteId) {
        //根据工艺id查询工艺路线的同时需要去查组成工序,与关联产品
        //1.先查工艺路线表
        CraftRoute craftRoute = craftRouteMapper.selectById(craftRouteId);
        CraftRouteVO craftRouteVO = new CraftRouteVO();
        BeanUtils.copyProperties(craftRoute,craftRouteVO);
        //2.查询中间表的工艺组成
        List<CraftConstituteVO> infoList = craftConstituteService.getInfoList(craftRouteId);
        craftRouteVO.setList(infoList);
        return craftRouteVO;
    }

    @Override
    public void updateInfoCraftRouteById(CraftRouteVO craftRouteVO) {
        CraftRoute craftRoute = new CraftRoute();
        BeanUtils.copyProperties(craftRouteVO,craftRoute);
        craftRouteMapper.updateById(craftRoute);
    }

    @Override
    public void deleteInfoCraftRouteById(String craftRouteId) {
        //删除工艺路线表,及其关联的表中的数据 组成工序表,关联产品表
        //1.先删除工艺路线
        int i = craftRouteMapper.deleteById(craftRouteId);
        //2.删除中间表信息
        craftConstituteService.deleteInfoCraftConstituteByCraftRouteId(craftRouteId);
        //3.删除工艺 物料中间表信息
        craftRelatedProductService.deleteInfoCraftRelatedCraftRouteId(craftRouteId);
    }
}
